package com.deer.wms.ware.task.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.constant.BaseSystemConstant;
import com.deer.wms.base.system.model.Cell.CellInfo;
import com.deer.wms.base.system.model.Pack.PackDetail;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.base.system.service.PackDetailService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.constant.InventoryConstant;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.model.Inventory.InventoryCriteria;
import com.deer.wms.inventory.model.Inventory.InventorySelectDto;
import com.deer.wms.inventory.model.Inventory.InventoryTransact;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.inventory.service.InventoryTransactService;
import com.deer.wms.inventory.service.SerialNoService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.project.root.util.MyUtils;
import com.deer.wms.project.root.util.StringUtil;
import com.deer.wms.project.root.util.StringUtils;
import com.deer.wms.system.manage.service.ValidatorService;
import com.deer.wms.ware.task.constant.WareTaskConstant;
import com.deer.wms.ware.task.model.Box.BoxDetail;
import com.deer.wms.ware.task.model.SO.DeliveryData;
import com.deer.wms.ware.task.model.SO.SoDetail;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.Wave.WaveDetail;
import com.deer.wms.ware.task.model.pickTask.*;
import com.deer.wms.ware.task.service.PickTaskService;
import com.deer.wms.ware.task.service.SoDetailService;
import com.deer.wms.ware.task.service.SoMasterService;
import com.deer.wms.ware.task.service.WaveDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @author guo
 * @since 2020/02/02
 */
@RestController
@RequestMapping("/pick/tasks")
public class PickTaskController {

    @Autowired
    private PickTaskService pickTaskService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private SoDetailService soDetailService;
    @Autowired
    private SoMasterService soMasterService;
    @Autowired
    private WaveDetailService waveDetailService;
    @Autowired
    private PackDetailService packDetailService;
    @Autowired
    private InventoryTransactService inventoryTransactService;
    @Autowired
    private ValidatorService validatorService;
    @Autowired
    private SerialNoService serialNoService;

    @PostMapping
    public Result add(@RequestBody PickTask pickTask) {
        pickTaskService.save(pickTask);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/manualCreatePickTask")
    public Result manualCreatePickTask(@RequestBody CreatePickTask createPickTask) {
        if (createPickTask.getBillNo().equals("") || createPickTask.getSoDetailId() == null || createPickTask.getCreatePickTaskDetails().size() <= 0) {
            return ResultGenerator.genFailResult(CommonCode.PARAMETER_ERROR);
        }
        List<PickTaskDto> list = pickTaskService.findList(new PickTaskCriteria(createPickTask.getSoDetailId(), 4));
        if (list.size() > 0) {
            return ResultGenerator.genFailResult(CommonCode.PICK_TASK_ALREADY_EXIST);
        }
        SoDetail soDetail = soDetailService.getById(createPickTask.getSoDetailId());
        SoMaster soMaster = soMasterService.findBy("bill_no", createPickTask.getBillNo());
        //分配数量
        double allottedQuantity = 0;
        for (CreatePickTaskDetail createPickTaskDetail : createPickTask.getCreatePickTaskDetails()) {
            InventorySelectDto inventorySelectDto = inventoryService.findByInventoryId(new InventoryCriteria(createPickTaskDetail.getInventoryId())).get(0);
            //发运单的单位
            PackDetail packDetail = packDetailService.getById(soDetail.getPackDetailId());
            //拣货任务的单位
            PackDetail packDetailPick = packDetailService.getById(createPickTaskDetail.getPackDetailId());
            /**
             * 涉及到转换率
             */
            double quantity = MyUtils.calculateDivide(soDetail.getPackDetailId(), inventorySelectDto.getPackDetailId(), createPickTaskDetail.getPickQuantity(), packDetail.getTransRatio());
            inventorySelectDto.setAllotQuantity((inventorySelectDto.getAllotQuantity() == null ? 0.0 : inventorySelectDto.getAllotQuantity()) + quantity);
            inventoryService.updateById(inventorySelectDto);
            PickTask pickTask = new PickTask(
                    soDetail.getSoDetailId(),
                    soMaster.getSoMasterId(),
                    soMaster.getWareId(),
                    soMaster.getWareName(),
                    soMaster.getOrganizationId(),
                    soMaster.getOrganizationName(),
                    inventorySelectDto.getAreaCode(),
                    inventorySelectDto.getAreaName(),
                    inventorySelectDto.getShelfCode(),
                    inventorySelectDto.getShelfName(),
                    inventorySelectDto.getCellCode(),
                    inventorySelectDto.getCellName(),
                    inventorySelectDto.getItemCode(),
                    inventorySelectDto.getItemName(),
                    inventorySelectDto.getBatchId(),
                    inventorySelectDto.getBatchName(),
                    inventorySelectDto.getBoxCode(),
                    inventorySelectDto.getLpn(),
                    soDetail.getPackDetailId(),
                    soDetail.getPackDescribe(),
                    createPickTaskDetail.getPickQuantity(),
                    0,
                    inventorySelectDto.getInventoryId(),
                    1,
                    soDetail.getImgUrl()
                    );
            pickTask.setPackDetailId(packDetailPick.getPackDetailId());
            pickTask.setPackDescribe(packDetailPick.getPackDescribe());
            pickTaskService.save(pickTask);
            allottedQuantity += createPickTaskDetail.getPickQuantity();
            Integer invenotryId =createPickTaskDetail.getInventoryId();
            Inventory inventory = inventoryService.getById(invenotryId);
            inventory.setAllotQuantity(createPickTaskDetail.getPickQuantity());
        }
        soDetail.setAllottedQuantity(allottedQuantity+soDetail.getAllottedQuantity());
        soDetailService.updateById(soDetail);

        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        pickTaskService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody PickTask pickTask) {
        pickTaskService.updateById(pickTask);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        PickTask pickTask = pickTaskService.getById(id);
        return ResultGenerator.genSuccessResult(pickTask);
    }

    @GetMapping("/list")
    public Result list(PickTaskCriteria criteria) {
        StringUtil.trimObjectStringProperties(criteria);
       // criteria.setWaveCode(criteria.getWaveCode().trim());
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<PickTaskDto> list = pickTaskService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findBySoMasterId")
    public Result findBySoMasterId(PickTaskCriteria criteria) {
        StringUtil.trimObjectStringProperties(criteria);
        // criteria.setWaveCode(criteria.getWaveCode().trim());
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<PickTaskDto> list = pickTaskService.findByBillNo(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findByWaveId")
    public Result findByWaveId(PickTaskCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<PickTaskDto> list = pickTaskService.findByWave(criteria.getWaveId());
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findByDetailId")
    public Result findByDetailId(@RequestParam Integer soDetailId) {
        List<PickTask> list = pickTaskService.findBySoDetailId(soDetailId);
        return ResultGenerator.genSuccessResult(list);
    }

    @GetMapping("/allotTask")
    public Result allotTask(PickTaskCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        StringUtil.trimObjectStringProperties(criteria);
        // criteria.setWaveCode(criteria.getWaveCode().trim());
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        criteria.setUserId(currentUser.getUserId());
        List<PickTaskDto> list = pickTaskService.allotTask(criteria);
        for (PickTaskDto pickTask:list  ) {
            if(pickTask.getAllotUserId()==null){
                pickTask.setAllotTime(DateUtils.getNowDateTimeString());
                pickTask.setAllotUserId(currentUser.getUserId());
                pickTask.setAllotUserName(currentUser.getUserName());
                pickTaskService.updateById(pickTask);
            }
        }
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/boxingList")
    public Result boxingList(PickTaskCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<PickTaskDto> list = new ArrayList<>();
        if (criteria.getTag() == 1) {
            list = pickTaskService.findBySo(criteria.getBillNo());

        } else if (criteria.getTag() == 2) {
            list = pickTaskService.findByWave(criteria.getWareId());
        }
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/findFirst")
    public Result findFirst(@RequestBody PickTaskCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<PickTaskDto> list = pickTaskService.findFirst(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/cancelPickTask")
    @Transactional
    public Result cancelPickTask(@RequestBody PickTaskCriteria pickTaskCriteria) {
        List<PickTaskDto> list = pickTaskService.findList(pickTaskCriteria);
        for (PickTaskDto pickTask : list) {
            SoDetail soDetail = soDetailService.getById(pickTask.getSoDetailId());
            soDetail.setAllottedQuantity(soDetail.getAllottedQuantity() - pickTask.getPickQuantity());
            soDetailService.updateById(soDetail);
            pickTask.setState(WareTaskConstant.PICK_STATE_CANCELED);
            pickTaskService.updateById(pickTask);
            Inventory inventory = inventoryService.getById(pickTask.getInventoryId());
            /**
             * 涉及到转换率
             */
            double quantity = MyUtils.calculateMultiplication(inventory.getPackDetailId(), pickTask.getPackDetailId(), pickTask.getPickQuantity(), pickTask.getTransRatio());
            inventory.setAllotQuantity(inventory.getAllotQuantity() - quantity);
            inventoryService.updateById(inventory);
        }
        return ResultGenerator.genSuccessResult();
    }

    @Autowired
    private CellInfoService cellInfoService;

    /**
     * 拣货任务 直接确认拣货
     */
    @PostMapping("/pickTaskAffirm")
    @Transactional
    public Result pickTaskAffirm(@RequestBody PickTaskCriteria pickTaskCriteria, @ApiIgnore @User CurrentUser currentUser) {
        //Step1.校验
        // 目标库位； 任务状态；
        String cellCode = pickTaskCriteria.getToCellCode();
        CellInfo cellInfo = cellInfoService.findByCodeAndWareId(cellCode, pickTaskCriteria.getWareId());
        if (ObjectUtil.notEqual(BaseSystemConstant.CELL_USE_TYPE_COLLECTION, cellInfo.getCellUseType())) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "该货位不是集货位，不支持拣货");
        }
        Integer pickTaskId = pickTaskCriteria.getPickTaskId();
        PickTask pickTask = pickTaskService.getById(pickTaskId);
        if (ObjectUtil.equals(WareTaskConstant.PICK_STATE_ALLPICK, pickTask.getState())) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "该任务已经拣货完成！");
        }
        if (Objects.equals(pickTask.getState(), WareTaskConstant.PICK_STATE_MERGED)) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "该任务已合并，不支持拣货");
        }

        List<PickTaskDto> pickTaskDtos = pickTaskService.getChildTasksByParentId(pickTaskId);
        if (pickTaskDtos.size() == 0) {
            pickTaskDtos.add(pickTaskService.getTaskById(pickTaskId));
        }
        if (pickTaskDtos.size() == 0) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "");
        }
        for (PickTaskDto pickTaskDto : pickTaskDtos) {
            //涉及到转换率
            Inventory inventory = inventoryService.getById(pickTaskDto.getInventoryId());
            double quantity = MyUtils.calculateMultiplication(inventory.getPackDetailId(), pickTaskDto.getPackDetailId(), pickTaskDto.getPickQuantity(), pickTaskDto.getTransRatio());
            inventory.setAllotQuantity(inventory.getAllotQuantity() - quantity);
            inventoryService.updateById(inventory);

            pickTask.setToCellCode(cellCode);
            pickTaskService.commitPick(pickTask, currentUser);
//            if(!CollectionUtils.isEmpty(pickTaskCriteria.getSerialNos())){
//                serialNoService.serialNoRecored(pickTaskCriteria.getSerialNos(), currentUser.getUserId(), currentUser.getUserName(),
//                        pickTaskDto.getPickTaskId(), inventory.getInventoryId(), InventoryConstant.SERIALNO_CHECKANDACCEPT);
//            }
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/warePickTask")
    public Result warePickTask(@RequestBody PickTaskCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        List<PickTaskDto> list = pickTaskService.findList(criteria);
        return ResultGenerator.genSuccessResult();
    }

    /*@PostMapping("/as")
    public Result as(@RequestBody PickTaskCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        String tableName = "so_master";
        Integer nextId = validatorService.getNextId(tableName);
        String mergeNo = "HB"+"-"+ nextId;
        System.out.print("hanhna");
        *//*for(int i=0;i<merge.getBillNos().size();i++){
            String billNo = merge.getBillNos().get(i);
            SoMaster soMaster = soMasterService.findBy("bill_no",billNo);
            soMaster.setMergeNo(mergeNo);
            soMaster.setState(11);
            soMasterService.updateById(soMaster);

            SoDetailCriteria criteria = new SoDetailCriteria();
            criteria.setBillNo(billNo);
            List<SoDetailDto> soDetails = soDetailService.findList(criteria);
            for(int y=0;y<soDetails.size();y++){
                SoDetail soDetail = soDetails.get(y);
                soDetail.setMergeNo(mergeNo);
                soDetailService.updateById(soDetail);
            }

        }

        SoMasterCriteria criteria = new SoMasterCriteria();
        criteria.setBillNos(merge.getBillNos());
        List<SoMaster> soMasters = soMasterService.findList(criteria);
        SoMaster soMaster = new SoMaster();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");

        Date d4 = dateFormat.parse(DateUtils.getNowDateTimeString());
        Date d5 = dateFormat.parse(DateUtils.getNowDateTimeString());
        Date d6 = dateFormat.parse(DateUtils.getNowDateTimeString());


        for(int i = 0; i < soMasters.size(); i++) {
            SoMaster soMaster1 = soMasters.get(i);

            String deliveryTime = soMaster1.getDeliveryTime();
            String expectTimeFm = soMaster1.getExpectTimeFm();
            String expectTimeTo = soMaster1.getExpectTimeTo();

            if (i == 0) {
                d4 = dateFormat.parse(deliveryTime);
                d5 = dateFormat.parse(expectTimeFm);
                d6 = dateFormat.parse(expectTimeTo);

                soMaster.setDeliveryTime(deliveryTime);
                soMaster.setExpectTimeFm(expectTimeFm);
                soMaster.setExpectTimeTo(expectTimeTo);
            }

            Date d1 = dateFormat.parse(deliveryTime);
            Date d2 = dateFormat.parse(expectTimeFm);
            Date d3 = dateFormat.parse(expectTimeTo);

            if (d1.before(d4)) {
                soMaster.setDeliveryTime(deliveryTime);
            }
            if (d2.before(d5)) {
                soMaster.setExpectTimeFm(expectTimeFm);
            }
            if (d3.before(d6)) {
                soMaster.setExpectTimeTo(expectTimeTo);
            }
            soMaster.setWareId(soMaster1.getWareId());
            soMaster.setWareName(soMaster1.getWareName());
            soMaster.setOrganizationId(soMaster1.getOrganizationId());
            soMaster.setOrganizationName(soMaster1.getOrganizationName());
            soMaster.setCustomerId(soMaster1.getCustomerId());
            soMaster.setCustomerName(soMaster1.getCustomerName());
            soMaster.setCarrierId(soMaster1.getCarrierId());
            soMaster.setCarrierName(soMaster1.getCarrierName());
            soMaster.setCreateTime(DateUtils.getNowDateTimeString());
            soMaster.setCreateUserId(currentUser.getUserId());
            soMaster.setCreateUserName(currentUser.getUserName());
            soMaster.setOrderNo(soMaster1.getOrderNo());
            soMaster.setInsertType(soMaster1.getInsertType());
            soMaster.setSoUdfHs1(soMaster1.getSoUdfHs1());
            soMaster.setSoUdfHs2(soMaster1.getSoUdfHs2());
            soMaster.setSoUdfHs3(soMaster1.getSoUdfHs3());
            soMaster.setMemo(soMaster1.getMemo());
            soMaster.setReceivingAddressId(soMaster1.getReceivingAddressId());
            soMaster.setLinkMan(soMaster1.getLinkMan());
            soMaster.setLinkPhone(soMaster1.getLinkPhone());
            soMaster.setProvince(soMaster1.getProvince());
            soMaster.setCity(soMaster1.getCity());
            soMaster.setArea(soMaster1.getArea());
            soMaster.setDetailAddress(soMaster1.getDetailAddress());


        }


        soMaster.setState(0);
        soMaster.setCaseState(2);
        soMaster.setBillNo(mergeNo);
        soMaster.setBillSource(2);
        soMaster.setMergeNo(null);

        soMasterService.save(soMaster);*//*

        return ResultGenerator.genSuccessResult("");
    }*/

    /**
     * 按订单复核
     *
     * @param boxDetail
     * @return
     */
    @PostMapping("/checkBySo")
    public Result checkBySo(@RequestBody BoxDetail boxDetail) {

        String time = DateUtils.getNowDateTimeString();

        PickTask pickTask = pickTaskService.getById(boxDetail.getPickTaskId());
        pickTask.setReviewQuantity(pickTask.getReviewQuantity() + boxDetail.getQuantity());
        pickTaskService.updateById(pickTask);

        SoDetail soDetail = soDetailService.getById(boxDetail.getSoDetailId());
        soDetail.setDeliveryQuantity(soDetail.getDeliveryQuantity() + boxDetail.getQuantity());
        soDetailService.updateById(soDetail);

        SoMaster soMaster = soMasterService.getById(boxDetail.getSoMasterId());
        soMaster.setActualDeliveryTime(time);
        soMasterService.updateById(soMaster);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "批量发货", notes = "批量发货")
    @PostMapping("/delivery")
    public Result delivery(@RequestBody DeliveryData deliveryData) {
        for (int i = 0; i < deliveryData.getPickTasks().size(); i++) {
            //step1.生成库存事务
            PickTask pickTask = deliveryData.getPickTasks().get(i);
            InventoryTransact inventoryTransact = new InventoryTransact();
            inventoryTransact.setTransactType(InventoryConstant.SERIALNO_OUT);
            inventoryTransact.setFromBatchId(pickTask.getBatchId());
            inventoryTransact.setFromBatchName(pickTask.getBatchName());
            inventoryTransact.setWareId(pickTask.getWareId());
            inventoryTransact.setWareName(pickTask.getWareName());
            inventoryTransact.setToCellCode(pickTask.getToCellCode());
            inventoryTransact.setToBatchId(pickTask.getBatchId());
            inventoryTransact.setToBatchName(pickTask.getBatchName());
            inventoryTransact.setItemCode(pickTask.getItemCode());
            inventoryTransact.setItemName(pickTask.getItemName());
            inventoryTransact.setToBoxCode(pickTask.getBoxCode());
            inventoryTransact.setToPackDetailId(pickTask.getPackDetailId());
            inventoryTransact.setToPackDescribe(pickTask.getPackDescribe());
            inventoryTransact.setToTransRatio(0.0);
            inventoryTransact.setCreateUserId(pickTask.getCheckUserId());
            inventoryTransact.setCreateUserName(pickTask.getCheckUserName());
            inventoryTransact.setCreateTime(pickTask.getCheckTime());
            inventoryTransact.setToQuantity(pickTask.getPickQuantity());
            inventoryTransact.setToOrganizationId(pickTask.getOrganizationId());
            inventoryTransact.setToOrganizationName(pickTask.getOrganizationName());
            inventoryTransactService.save(inventoryTransact);

            //step2.修改SO明细的状态 部分发货、全部发货
            Integer soDetailId = pickTask.getSoDetailId();
            SoDetail soDetail = soDetailService.getById(soDetailId);
            soDetail.setDeliveryQuantity(pickTask.getPickQuantity());
            if (soDetail.getDeliveryQuantity().equals(soDetail.getOrderQuantity())) {
                soDetail.setState(2); //全部发货
            } else {
                soDetail.setState(1); //部分发货

            }
            soDetailService.updateById(soDetail);

            inventoryTransact.setDetailNo(soDetail.getDetailNo());
            inventoryTransactService.updateById(inventoryTransact);

            //step3.修改拣货任务状态
            pickTask.setState(WareTaskConstant.PICK_STATE_OUTED);
            pickTaskService.updateById(pickTask);

            //step4.修改库存
            Integer inventoryId = pickTask.getInventoryId();
            Inventory inventory = inventoryService.getById(inventoryId);
            inventory.setAllotQuantity(inventory.getAllotQuantity() - pickTask.getPickQuantity());
            inventory.setQuantity(inventory.getQuantity() - pickTask.getPickQuantity());
            inventoryService.updateById(inventory);
            if (inventory.getQuantity() == 0) {
                inventoryService.removeById(inventoryId);
            }

            //step5.修改so单状态
            List<SoDetail> soDetailList = soDetailService.findByBillNo(soDetail.getBillNo());
            Boolean soDelivery = true;
            SoMaster soMaster = soMasterService.findBy("bill_no", soDetail.getBillNo());
            for (SoDetail soDetail1 : soDetailList) {
                if (soDetail1.getState() != 3) {
                    soDelivery = false;
                }
                soMaster.setState(7);//部分发货
                soMasterService.updateById(soMaster);
            }
            if (soDelivery) {
                soMaster.setState(8);//全部发货
                soMasterService.updateById(soMaster);
            }
        }

        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/getPickTasksForSeeding")
    public Result getPickTasksForSeeding(String waveCode, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER);
        }
        PickTaskCriteria criteria = new PickTaskCriteria();
        criteria.setWaveCode(waveCode);
        List<PickTaskDto> list = pickTaskService.findList(criteria);
        return ResultGenerator.genSuccessResult(new PageInfo(list));
    }
    @GetMapping("/findByWaveMasterId")
    public Result findByWaveMasterId(PickTaskCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());

        List<PickTaskDto> list = pickTaskService.findByWaveMasterId(criteria);
        return ResultGenerator.genSuccessResult(new PageInfo(list));
    }
    @PostMapping("/getSeedingWallAlertInfo")
    public Result getSeedingWallAlertInfo(@RequestBody PickTaskCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER);
        }
        List<SeedingWallAlertInfo> list = pickTaskService.getSeedingWallAlertInfo(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    @Transactional
    @GetMapping("/updateSeededQuantity")
    public Result updateSeededQuantity(String soNo, String itemCode, Double quantity, Integer inventoryId, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER);
        }
        PickTaskCriteria taskCriteria = new PickTaskCriteria();
        taskCriteria.setBillNo(soNo);
        taskCriteria.setItemCode(itemCode);
        taskCriteria.setInventoryId(inventoryId);
        List<PickTaskDto> pickTasks = pickTaskService.findList(taskCriteria);
        for (PickTask pickTask : pickTasks) {
            Double pickQuantity = pickTask.getPickQuantity();
            Double seededQuantity = pickTask.getSeededQuantity();
            Double neededQuantity = pickQuantity - seededQuantity;
            if (quantity > neededQuantity) {
                pickTask.setSeededQuantity(pickQuantity);
                quantity -= neededQuantity;
            } else if (quantity <= neededQuantity) {
                pickTask.setSeededQuantity(seededQuantity + quantity);
                pickTaskService.updateById(pickTask);
                break;
            }
            pickTaskService.updateById(pickTask);
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "拣货确认", notes = "PDA")
    @ApiImplicitParams({@ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class, required = true)})
    @PostMapping("/commitPick")
    public Result commitPick(@RequestBody CommitPickTask commitPickTask, @ApiIgnore @User CurrentUser currentUser) {
        //Step1.校验
        // 目标容器是否有值，没有值则取系统的默认值
        // 拣货任务状态
        Integer pickTaskId = commitPickTask.getPickTaskId();
        PickTask pickTask = pickTaskService.getById(pickTaskId);
        if (WareTaskConstant.PICK_STATE_ALLPICK.equals(pickTask.getState())) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "该拣货任务已拣货完毕!");
        }

        String toCellCode = commitPickTask.getToCellCode();
        if (StringUtils.isEmpty(toCellCode)) {
            toCellCode = cellInfoService.getSysJhCell(commitPickTask.getWareId());
        }
        pickTask.setToCellCode(toCellCode);
        pickTask.setToBoxCode(commitPickTask.getToBoxCode());

        //Step2.拣货确认
        pickTaskService.commitPick(pickTask, currentUser);

        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "发货复核", notes = "支持按单发货和波次发货两种场景")
    @GetMapping("/getBoxesForSend")
    @Transactional
    public Result getBoxesForSend(@RequestParam String boxCode) {
        //Step1.校验
        //判断库存，判断是否在发货区
        Inventory inventory = inventoryService.getOneByField("box_code", boxCode);
        if (ObjectUtil.isNull(inventory)) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "容器" + boxCode + "查询不到库存");
        }

        String cellCode = inventory.getCellCode();
        Integer cellType = cellInfoService.getCellType(cellCode, inventory.getWareId());
        if (ObjectUtil.notEqual(BaseSystemConstant.CELL_USE_TYPE_COLLECTION, cellType)) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "容器库存" + boxCode + "不在发货区");
        }
        //Step2.查询
        List<PickTask> list;
        PickTask pickTask = pickTaskService.getOneByField("inventory_id", inventory.getInventoryId());
        if (ObjectUtil.isNotNull(pickTask.getSoMasterId())) {
            //按单发货逻辑
            SoMaster soMaster = soMasterService.getById(pickTask.getSoMasterId());
            if (WareTaskConstant.SO_STATE_PICKED.equals(soMaster.getState())) {
                list = pickTaskService.getListByField("so_master_id", soMaster.getSoMasterId());
            } else {
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "未全部拣货");
            }
            //用于前台显示对应的出库任务
            list.get(0).setMemo(soMaster.getBillNo());
        } else {
            //波次发货逻辑
            Integer pickUserId = pickTask.getPickUserId();
            //根据拣货人查询到执行中的波次明细
            QueryWrapper<WaveDetail> qw = new QueryWrapper<>();
            qw.eq("pick_user_id", pickUserId);
            qw.in("state", WareTaskConstant.WAVE_STATE_PICKING, WareTaskConstant.WAVE_STATE_ALL_ALOT);
            List<WaveDetail> waveDetails = waveDetailService.list(qw);
            List<Integer> waveIds = waveDetails.stream().map(WaveDetail::getWaveDetailId).collect(Collectors.toList());

            //找到波次明细对应的拣货任务
            QueryWrapper<PickTask> qw2 = new QueryWrapper<>();
            qw2.eq("state", WareTaskConstant.PICK_STATE_ALLPICK);
            qw2.in("wave_detail_id", waveIds);

            list = pickTaskService.list(qw2);
        }
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation(value = "查询拣货单")
    @PostMapping("/picking/list")
    public Result selectPickingList(@RequestBody PickTaskCriteria criteria) {
        // 设置页面的分页条件
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        // 使用MybatisPlus的QueryWrapper进行查询就不用自己写sql了
        QueryWrapper<PickTask> qw = new QueryWrapper<>();
//        Map<String, Object> map = new HashMap<>(3);
        // 设置状态为 0-待拣货 (0-待拣货  1-部分拣货  2-拣货完成  3-取消拣货  4-被合并 5-已发运)
        criteria.setState(WareTaskConstant.PICK_STATE_PROCESSING);
        // 设置查询条件
//        map.put("ware_id", criteria.getWareId());
//        map.put("so_master_id", criteria.getFindBySoMasterId());
//        map.put("state",criteria.getState());
        qw.eq("ware_id",criteria.getWareId());
        if(ObjectUtil.isNotNull(criteria.getFindBySoMasterId())){
            qw.eq("so_master_id",criteria.getFindBySoMasterId());
        }
        qw.ne("state",criteria.getState());
//      List<PickTaskVo> pickingList = pickTaskService.list(qw.allEq(map, false));
        List<PickTask> pickingList = pickTaskService.list(qw);
        // 封装分页的数据
        PageInfo pageInfo = new PageInfo(pickingList);
        // 返回成功的结果
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
